package com.qf.xiaomi.dao.impl;

import com.qf.xiaomi.dao.goodsTypeDao;
import com.qf.xiaomi.pojo.TbGoodsType;
import com.qf.xiaomi.utils.DButils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class goodsTypeDaoImpl implements goodsTypeDao {
    QueryRunner queryRunner = new QueryRunner(DButils.getdataSource());
    @Override
    public List<TbGoodsType> selectType() {
        String sql = "select * from tb_goods_type where status=1";
//        String sql = "select * from tb_goods_type where level=1 and status=1";
        try {
            List<TbGoodsType> goodsTypes = queryRunner.query(sql, new BeanListHandler<>(TbGoodsType.class));
            return goodsTypes;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addType(TbGoodsType type) {
        String sql = "insert into tb_goods_type (name,parent)values(?,?)";
        try {
            int count = queryRunner.update(sql, type.getName(), type.getParent());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public TbGoodsType findNameById(Long typeId) {
        String sql = "select * from tb_goods_type where id = ?";
        try {
            TbGoodsType Type = queryRunner.query(sql, new BeanHandler<>(TbGoodsType.class), typeId);
            return Type;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void delBytTypeId(Long typeId) {
        String sql = "delete from tb_goods_type where id=?";
        try {
            int update = queryRunner.update(sql, typeId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
